package com.moneywelcome.results;

import com.baomidou.mybatisplus.core.metadata.IPage;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
import lombok.Getter;
import lombok.ToString;

import java.util.List;

@ApiModel("分页查询统一返回格式")
@Getter
@ToString
public class PageResult<T> {
    @ApiModelProperty("页码")
    private long pageNumber;
    @ApiModelProperty("页面大小")
    private long pageSize;
    @ApiModelProperty("数据总数")
    private long totalCount;
    @ApiModelProperty("当前页面数据")
    private List<T> data;

    /**
     * 传入一个Ipage，返回一个统一形式结果
     */
    public static PageResult getPageResult(IPage page){
        return getInstance()
                .setData(page.getRecords())
                .setPageNumber(page.getCurrent())
                .setPageSize(page.getSize())
                .setTotalCount(page.getTotal());
    }


    public static PageResult getInstance(){
        return new PageResult();
    }

    public PageResult setPageNumber(long pageNum) {
        this.pageNumber = pageNum;
        return this;
    }

    public PageResult setPageSize(long pageSize) {
        this.pageSize = pageSize;
        return this;
    }

    public PageResult setTotalCount(long totalCount) {
        this.totalCount = totalCount;
        return this;
    }

    public PageResult<T> setData(List<T> data) {
        this.data = data;
        return this;
    }
}
